﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Web;


namespace MediaMonitor.Common
{

    public class App
    {

        public static void ReloadAppData()
        {
            GetAppDataSet(true);
        }

        private static DataSet GetAppDataSet(bool reload)
        {

            if ((HttpContext.Current.Application["AppDataSet"] == null) || reload == true)
            {
                //Dim ds As DataSet = LWD.SPs.GetAllVCmsPages().GetDataSet
                DataSet ds = new DataSet("AppDataSet");


                //Below write code for loading data tables into AddDataSet



                ds.Tables.Add(Adex.Medium.Query().ExecuteDataSet().Tables[0].Copy());
                ds.Tables[0].TableName = "Medium";

                HttpContext.Current.Application.Lock();
                HttpContext.Current.Application.Remove("AppDataSet");
                HttpContext.Current.Application["AppDataSet"] = ds;
                HttpContext.Current.Application.UnLock();

                return ds;

            }
            else
            {
                return (DataSet)HttpContext.Current.Application["AppDataSet"];
            }
        }

        public static int GetMediaTypeCode(int mediaCode)
        {
            DataRow[] drs = GetAppDataSet(false).Tables["Medium"].Select("MediaCode=" + mediaCode);
            if (drs.Length > 0)
                return Common.Utility.ToInt(drs[0]["MediaTypeCode"]);

            return 0;
        }



        #region "CMS"

        //private static void LoadCMSTable(ref DataSet ds, string tableName)
        //{
        //    DataTable dt = BusinessLogic.LWD.VCMSPage.Query.WHERE("PageActive", true).ExecuteDataSet.Tables(0);

        //    dt.TableName = tableName;
        //    ds.Tables.Add(dt.Copy());
        //}

        //public static DataView GetParentMenu(string PanelName)
        //{
        //    if (HttpContext.Current.Request.IsAuthenticated) {
        //        DataView dv = new DataView(GetAppDataSet().Tables["CmsPages"], "CmsPanelName='" + PanelName + "' AND ParentPageId IS NULL AND ShowIfLoggedIn=1", "Priority", DataViewRowState.CurrentRows);
        //        return dv;
        //    } else {
        //        DataView dv = new DataView(GetAppDataSet().Tables["CmsPages"], "CmsPanelName='" + PanelName + "' AND ParentPageId IS NULL", "Priority", DataViewRowState.CurrentRows);
        //        return dv;
        //    }

        //}

        //public static DataView GetChildMenu(string PanelName, int ParentPageId)
        //{
        //    if (HttpContext.Current.Request.IsAuthenticated) {
        //        return new DataView(GetAppDataSet().Tables["CmsPages"], "CmsPanelName='" + PanelName + "' AND ParentPageId=" + ParentPageId + " AND ShowIfLoggedIn=1", "Priority", DataViewRowState.CurrentRows);
        //    } else {
        //        return new DataView(GetAppDataSet().Tables["CmsPages"], "CmsPanelName='" + PanelName + "' AND ParentPageId=" + ParentPageId, "Priority", DataViewRowState.CurrentRows);
        //    }

        //}

        //public static DataRow[] GetChildMenuRows(string PanelName, int parentPageId)
        //{
        //    if (HttpContext.Current.Request.IsAuthenticated) {
        //        return GetAppDataSet().Tables["CmsPages"].Select("CmsPanelName='" + PanelName + "' AND ParentPageId=" + parentPageId + " AND ShowIfLoggedIn=1", "Priority");
        //    } else {
        //        return GetAppDataSet().Tables["CmsPages"].Select("CmsPanelName='" + PanelName + "' AND ParentPageId=" + parentPageId, "Priority");
        //    }

        //}

        //public static DataView GetPanelLinks(string PanelName)
        //{
        //    return new DataView(GetAppDataSet().Tables["CmsPages"], "CmsPanelName='" + PanelName + "'", "Priority", DataViewRowState.CurrentRows);
        //}

        //public static DataRowView GetCmsPage(int PageId)
        //{
        //    DataView dv = new DataView(GetAppDataSet().Tables["CmsPages"], "PageId=" + PageId, "Priority", DataViewRowState.CurrentRows);
        //    if (dv.Count > 0) {
        //        return dv[0];
        //    } else {
        //        return null;
        //    }
        //}

        //public static int GetCmsPageIdByUrl(string url)
        //{
        //    //url = Regex.Replace(url, "[\?&](?<name>[^&=]*)=(?<value>[^&=]*)", "").Replace(AppPath, "").Replace("/LWD/", "").Replace("", "").Replace(Constants.REWRITE_EXTENTION, "")


        //    DataView dv = new DataView(GetAppDataSet().Tables["CmsPages"], "Url LIKE '%" + url + "%' OR RedirectUrl LIKE '%" + url + ".aspx%'", "Priority", DataViewRowState.CurrentRows);

        //    int pageId = 5000;
        //    foreach (DataRowView dr in dv) {
        //        if (dr["PageId"] < pageId) {
        //            pageId = dr["PageId"];
        //        }
        //    }
        //    if (pageId > 0 & pageId < 5000) {
        //        return pageId;
        //    } else {
        //        return 0;
        //    }
        //}

        //public static int GetCmsPageIdByRedirectUrl(string redirectUrl)
        //{
        //    //url = Regex.Replace(url, "[\?&](?<name>[^&=]*)=(?<value>[^&=]*)", "").Replace(AppPath, "").Replace("/LWD/", "").Replace("", "").Replace(Constants.REWRITE_EXTENTION, "")


        //    DataView dv = new DataView(GetAppDataSet().Tables["CmsPages"], "RedirectUrl LIKE '%" + redirectUrl + "%'", "Priority", DataViewRowState.CurrentRows);

        //    int pageId = 5000;
        //    foreach (DataRowView dr in dv) {
        //        if (dr["PageId"] < pageId) {
        //            pageId = dr["PageId"];
        //        }
        //    }
        //    if (pageId > 0 & pageId < 5000) {
        //        return pageId;
        //    } else {
        //        return 0;
        //    }
        //}

        #endregion


    }

}